﻿//
// Created by @author 吴子昂 on 2025/11/4
//

#include "videoProcess.h"

videoProcess::videoProcess()
= default;

videoProcess::~videoProcess()
= default;

/*
* 对对象里面的图像进行高斯模糊处理并且开运算
*
* @return 处理是否完成
* */
bool videoProcess::preProcessBlurred()
{
    if (frame.empty())
    {
        return false;
    }

    // 高斯模糊处理，出去相机噪声
    GaussianBlur(frame, processedFrame, Size(5, 5), 0);

    // 开运算，去除椒盐噪声
    Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
    morphologyEx(processedFrame, processedFrame, MORPH_OPEN, kernel);

    return true;
}

Mat videoProcess::getFrame()
{
    return frame;
}

// @Getter
void videoProcess::showFrame()
{
    imshow("video", frame);
}

// @Setter
void videoProcess::setFrame(const Mat& frame)
{
    this->frame = frame;
}

/*
* 对对象里面的图像进行hsv颜色分割并且使用Canny边缘检测检测边缘
*
* @param hLow Hue值的最小值
* @param hHigh Hue值的最大值
* @param sLow  Saturation值的最小值
* @param sHigh Saturation值的最大值
* @param vLow Value值的最小值
* @param vHigh Value值的最大值
* @return 处理是否完成
* */
bool videoProcess::preProcessHSVCanny(int hLow, int hHigh, int sLow, int sHigh, int vLow, int vHigh)
{
    if (frame.empty())
    {
        return false;
    }

    Mat capH, capS, capV, hsvFrame;
    cvtColor(frame, hsvFrame, COLOR_BGR2HSV);
    vector<Mat> hsvSpace;
    split(hsvFrame, hsvSpace);
    capH = hsvSpace[0];
    capS = hsvSpace[1];
    capV = hsvSpace[2];

    Mat maskH, maskS, maskV;
    inRange(capH, hLow, hHigh, maskH);
    inRange(capS, sLow, sHigh, maskS);
    inRange(capV, vLow, vHigh, maskV);

    Mat maskHS, mask;
    Mat frameOut;
    bitwise_and(maskH, maskS, maskHS);
    bitwise_and(maskHS, maskV, mask);
    bitwise_and(frame, frame, frameOut, mask);

    Mat gray;
    cvtColor(frameOut, gray, COLOR_BGR2GRAY);

    Canny(gray, frameOut, 50, 150);
    Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
    morphologyEx(frameOut, frameOut, MORPH_CLOSE, kernel);

    processedFrame = frameOut;

    return true;
}

// 显示对象中的图像
void videoProcess::showProcessedFrame()
{
    imshow("processedFrame", processedFrame);
}

// @Getter
Mat videoProcess::getProcessedFrame()
{
    return processedFrame;
}

/*
* 设置图像的宽度和高度
*
* @param width 设置的图像的宽度
* @param height 设置的图像的高度
* */
void videoProcess::setSize(int width, int height)
{
    resize(frame, frame ,Size(width,height));
}
